$(function () {
    var dictionary_datagrid = $("#dictionary_datagrid");
    var dictionaryItem_datagrid = $("#dictionaryItem_datagrid");
    var dic_dialog = $("#dic_dialog");
    var dic_form = $("#dic_form");

    dictionary_datagrid.datagrid({
        url: '/dictionary/selectAll.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'sn', title: '字典目录编码', width: 100},
            {field: 'name', title: '字典目录名称', width: 100},
            {field: 'intro', title: '字典目录介绍', width: 100}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#dic_btns",
        rownumbers:true,
        onSelect: function (index, row) {
            var id = row.id;
            dictionaryItem_datagrid.datagrid({url: "/dictionaryItem/getItemByDictionaryId.do?dictionaryId=" + id})
        }
    });

    dic_dialog.dialog({
        width: 300,
        height: 300,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            dic_form.form('clear');
        }
    });
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    });
    var methodObject = {
        add: function () {
            dic_dialog.dialog('setTitle', '新增字典');
            dic_dialog.dialog('open');
        },

        edit: function () {
            var row = dictionary_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            dic_form.form('load', row );
            dic_dialog.dialog('setTitle', '编辑字典');
            dic_dialog.dialog('open');
        },
        del: function () {
            var row = dictionary_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/dictionary/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info');
                            dictionary_datagrid.datagrid('reload');
                            dictionaryItem_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            dictionary_datagrid.datagrid('reload');
        },

        save: function () {
            dic_form.form('submit', {
                url: '/dictionary/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info');
                        methodObject.cancel();
                        dictionary_datagrid.datagrid('reload');
                        $("#dictionaryName-combobox").combobox('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            dic_dialog.dialog('close');
        }

    }
})


